package moi.savecanalblog;

import java.net.URL;
import java.util.ArrayList;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.oro.text.regex.MalformedPatternException;
import org.apache.oro.text.regex.MatchResult;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.PatternMatcherInput;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;

public class HTTPReader {
	
	static{
		client = new HttpClient();
	}
	
	public static String getPageContent(URL inURL){

	    // Create a method instance.
	    GetMethod method = new GetMethod(inURL.toString());
	    
	    try {
	        // Execute the method.
	        int statusCode = client.executeMethod(method);

	        if (statusCode != HttpStatus.SC_OK) {
	          System.err.println("Method failed: " + method.getStatusLine());
	        }
	        
	        // Read the response body.
	        return method.getResponseBodyAsString();	        

	      } catch (HttpException e) {
	        System.err.println("Fatal protocol violation: " + e.getMessage());
	        e.printStackTrace();
	      } catch (java.io.IOException e) {
	        System.err.println("Fatal transport error: " + e.getMessage());
	        e.printStackTrace();
	      } finally {
	        // Release the connection.
	        method.releaseConnection();
	      } 
	      
	      return null;
	}
	
	public static ArrayList<String> getMatches(String inInput, String inPattern, int inChosenGroup){
	 int groups;
	 PatternMatcher matcher;
	 PatternCompiler compiler;
	 Pattern pattern;
	 PatternMatcherInput input;
	 MatchResult result;
	 ArrayList<String> results = new java.util.ArrayList<String>();
	 
	 compiler = new Perl5Compiler();
	 matcher  = new Perl5Matcher();

	 try {
	   pattern = compiler.compile(inPattern,Perl5Compiler.CASE_INSENSITIVE_MASK |Perl5Compiler.SINGLELINE_MASK);
	 } catch(MalformedPatternException e) {
	   System.out.println("Bad pattern.");
	   System.out.println(e.getMessage());
	   return null;
	 }

	 input   = new PatternMatcherInput(inInput);

	 while(matcher.contains(input, pattern)) {
	   result = matcher.getMatch();  

	   //results.add(result.toString());
	   
	   groups = result.groups();
	   if(inChosenGroup>0 && inChosenGroup<=groups){
		   
		   results.add(result.group(inChosenGroup));
		   
	   }else{
		   results.add(result.toString());
	   }
	 }
	 
	 return results;
	}
	
	public static byte[] getBinaryMedia(URL inMediaURL){
	    // Create a method instance.
	    GetMethod method = new GetMethod(inMediaURL.toString());
	    
	    try {
	        // Execute the method.
	        int statusCode = client.executeMethod(method);

	        if (statusCode != HttpStatus.SC_OK) {
	          System.err.println("Method failed: " + method.getStatusLine());
	        }
	        
	        // Read the response body.
	        return method.getResponseBody();        

	      } catch (HttpException e) {
	        System.err.println("Fatal protocol violation: " + e.getMessage());
	        e.printStackTrace();
	      } catch (java.io.IOException e) {
	        System.err.println("Fatal transport error: " + e.getMessage());
	        e.printStackTrace();
	      } finally {
	        // Release the connection.
	        method.releaseConnection();
	      } 
	      
	     return null;
	}
	
	
	private static HttpClient client;
}
